package com.zmzncs.lmtc.module.system.controller;

import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.system.pojo.permission.Permission;
import com.zmzncs.lmtc.module.system.pojo.permission.PermissionVO;
import com.zmzncs.lmtc.module.system.pojo.permission.PermissionSyncForm;
import com.zmzncs.lmtc.module.system.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2019-12-24
 */
@Api(tags = "权限")
@RestController
@RequestMapping("/system/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "查询权限树")
    @GetMapping(value = "getPermissionTree")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true, dataType = "String"),
    })
    public Rest<PermissionVO> getPermissionTree() {
        List<PermissionVO> permissionVOList = permissionService.getPermissionTree();

        return RestUtil.success(permissionVOList);
    }

    @ApiOperation(value = "同步权限")
    @PostMapping(value = "syncPermission")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true, dataType = "String"),
    })
    @Transactional
    public Rest<List<PermissionSyncForm>> syncPermission(@RequestBody List<PermissionSyncForm> permissionSyncFormList) {
        permissionService.remove(null);

        savePermission(permissionSyncFormList, 0);

        return RestUtil.success();
    }

    public void savePermission(List<PermissionSyncForm> permissionSyncFormList, Integer pid) {
        for(int i=0; i<permissionSyncFormList.size(); i++) {
            Permission permission = new Permission();
            BeanUtils.copyProperties(permissionSyncFormList.get(i), permission);
//            permission.setId((pid * 100) + i + 1);
            permission.setPid(pid);
            permission.setType("menu");
            permissionService.save(permission);

            if(permissionSyncFormList.get(i).getChildren() != null){
                savePermission(permissionSyncFormList.get(i).getChildren(), permission.getId());
            }
            if(permissionSyncFormList.get(i).getOperationList() != null){
                saveOperation(permissionSyncFormList.get(i).getOperationList(), permission.getId());
            }
        }
    }

    public void saveOperation(List<PermissionSyncForm.PermissionOperation> operationList, Integer pid) {
        for(int i=0; i<operationList.size(); i++) {
            Permission permission = new Permission();
            BeanUtils.copyProperties(operationList.get(i), permission);
            permission.setId((pid * 10) + i + 1);
            permission.setPid(pid);
            permission.setType("operation");
            permissionService.save(permission);

            if(operationList.get(i).getOperationList() != null){
                saveOperation(operationList.get(i).getOperationList(), permission.getId());
            }
        }
    }

}

